package com.task;

import cn.hutool.http.HttpUtil;
import com.admin.entity.GreenPMS;
import com.admin.entity.Terminal;
import com.admin.mapper.GreenPMSMapper;
import com.admin.mapper.TerminalMapper;
import com.admin.service.impl.StationServiceImpl;
import com.admin.service.impl.TerminalServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.service.v1.TerminalApiService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.util.MD5Util;
import com.util.StringUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 绿联pms
 */
@Service
public class GreenPmsTask {

    @Autowired
    private GreenPMSMapper greenPMSMapper;

    @Autowired
    private TerminalMapper terminalMapper;

    @Autowired
    private TerminalServiceImpl terminalService;

    private  String url = "https://bms.mehoodhotels.com/api/ai/hotel/";

    @Scheduled(cron = "*/59 */2 * * * ?")
    public void green()
    {

        QueryWrapper q = new QueryWrapper();
        q.eq("status",1);
        List<GreenPMS> list =  greenPMSMapper.selectList(q);
        for (GreenPMS pms:list){

            String key = pms.getKey();
            String hotelID= pms.getHotelId();
            String room = "all";
            Integer stationId = pms.getStationId();
            //String sing = "hotelId=值&key=值&rmno=值组合后进行md5";
            StringBuffer sb = new StringBuffer();
            sb.append("hotelId=").append(hotelID);
            sb.append("&key=").append(key);
            sb.append("&rmno=").append(room);
            String sign = MD5Util.getMD5(sb.toString()).toLowerCase();
            //System.out.println(sign);
            Map map = new HashMap();
            map.put("id",hotelID);
            map.put("room",room);
            map.put("sign",sign);
            String result = null;

            try {
                result = HttpUtil.get(url,map);
            }catch (Exception e){

            }

            //System.out.println(result);

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("terminal_station",stationId);
            List<Terminal> termalList = terminalMapper.selectList(queryWrapper);
            for (Terminal t: termalList){
                t.setTerminalPmsName(null);
                ///terminalMapper.updateById(t);
                terminalService.updateById(t);
            }

            JSONObject jsonObjectRoot = JSONObject.parseObject(result);
            if (jsonObjectRoot!=null){
                int status = jsonObjectRoot.getIntValue("status");
                if (status==0)
                {
                    JSONArray jsonArray = jsonObjectRoot.getJSONArray("result");
                    if (jsonArray!=null)
                    {

                        for (int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            JSONObject jsonObjectGuest = jsonObject.getJSONObject("guests");
                            if (jsonObjectGuest!=null) {
                                String name = jsonObjectGuest.getString("name");
                                String rmno = jsonObject.getString("rmno");
                                String idNo = jsonObjectGuest.getString("idNo");
                                String _sex = "";
                                String sex ="";
                                if (idNo!=null && idNo.trim().length()>=18){
                                     sex = idNo.substring(16,17);
                                    if (Integer.parseInt(sex)%2==1){
                                        _sex = "先生";
                                    }
                                    else{
                                        _sex ="女士";
                                    }
                                }
                                //System.out.println(stationId+"\t"+rmno+"\t"+name);
                                for (Terminal t: termalList){
                                    String t_room = t.getTerminalRoom();
                                    if (t_room!=null && t_room.equals(rmno)){
                                        //System.out.println(idNo+"\t"+name+sex);
                                        Date depTime = jsonObjectGuest.getSqlDate("dep");

                                        if (depTime.getTime()<System.currentTimeMillis()){
                                            t.setTerminalPmsName(null);
                                        }
                                        else{
                                            t.setTerminalPmsName(name+_sex);
                                        }
                                        //terminalMapper.updateById(t);
                                        terminalService.updateById(t);
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }



        }
    }




}


